//
//  AuctionMessageVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/5.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionMessageVC.h"
#import "AuctionPubNotisVC.h"
#import "AuctionLogisticsVC.h"
#import "KeFuViewController.h"

#pragma mark -- mark 数据相关
#import "AuctionMsgPublicNotisModel.h"
#import "AuctionLogisticsModel.h"

@interface AuctionMessageVC (){
    UILabel *_public_des_label;
    UILabel *_public_date_label;
    
    UILabel *_logistics_des_label;
    UILabel *_logistics_date_label;
}

@property (nonatomic, strong) UIView *publicNoticeView;
@property (nonatomic, strong) UIView *logisticsView;
@property (nonatomic, strong) UIView *customerView;

@property (nonatomic, strong) NSArray <AuctionMsgPublicNotisInfoModel *>* publicNotisData;
@property (nonatomic, strong) NSArray <AuctionLogisticsInfoModel *>* logisticsData;
@end

@implementation AuctionMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"消息";
    self.view.backgroundColor = SKColorWithHex(0xF7F7F7);
    [self initContentViews];
    
    [self requestPublicNotisData];
    [self requestLogisticData];
}

- (void)initContentViews {
    [self.publicNoticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.height.mas_equalTo(self.view.mas_width).multipliedBy(74.0/375);
    }];
    
    [self.logisticsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(self.publicNoticeView.mas_bottom).mas_offset(0.5);
        make.height.mas_equalTo(self.publicNoticeView);
    }];
    [self.customerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(self.logisticsView.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(self.logisticsView);
    }];
    
}

#pragma mark -- mark 懒加载
- (UIView *)publicNoticeView {
    if (!_publicNoticeView) {
        _publicNoticeView = [[UIView alloc] init];
        _publicNoticeView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_publicNoticeView];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_publick_notic_img")];
        [_publicNoticeView addSubview:imageView];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"官方公告";
        label.textColor = SKColorWithHex(0x232E45);
        label.font = SKFONT_Medium(15);
        [_publicNoticeView addSubview:label];
        UILabel *detailLabel = [[UILabel alloc] init];
        detailLabel.font = SKFONT_SIZE(13);
        detailLabel.textColor = SKColorWithHex(0x999999);
        detailLabel.text = @" ";
        [_publicNoticeView addSubview:detailLabel];
        UILabel *dateLabel = [[UILabel alloc] init];
        dateLabel.textColor = SKColorWithHex(0x999999);
        dateLabel.font = SKFONT_SIZE(10);
        dateLabel.text = @" ";
        _public_des_label = detailLabel;
        _public_date_label = dateLabel;
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [_publicNoticeView addGestureRecognizer:tap];
        [_publicNoticeView addSubview:dateLabel];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_offset(15);
            make.bottom.mas_offset(-15);
            make.width.mas_equalTo(imageView.mas_height);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).mas_offset(15);
            make.top.mas_equalTo(imageView);
        }];
        [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label);
            make.bottom.mas_equalTo(imageView);
            make.right.mas_lessThanOrEqualTo(-15);
            make.height.mas_equalTo(14);
        }];
        [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.top.mas_equalTo(imageView);
        }];
    }
    return _publicNoticeView;
}

- (UIView *)logisticsView {
    if (!_logisticsView) {
        _logisticsView = [[UIView alloc] init];
        _logisticsView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_logisticsView];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_logistics_msg_img")];
        [_logisticsView addSubview:imageView];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"物流信息";
        label.textColor = SKColorWithHex(0x232E45);
        label.font = SKFONT_Medium(15);
        [_logisticsView addSubview:label];
        UILabel *detailLabel = [[UILabel alloc] init];
        detailLabel.font = SKFONT_SIZE(13);
        detailLabel.textColor = SKColorWithHex(0x999999);
        detailLabel.text = @" ";
        [_logisticsView addSubview:detailLabel];
        UILabel *dateLabel = [[UILabel alloc] init];
        dateLabel.textColor = SKColorWithHex(0x999999);
        dateLabel.font = SKFONT_SIZE(10);
        dateLabel.text = @" ";
        _logistics_des_label = detailLabel;
        _logistics_date_label = dateLabel;
        [_logisticsView addSubview:dateLabel];
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [_logisticsView addGestureRecognizer:tap];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_offset(15);
            make.bottom.mas_offset(-15);
            make.width.mas_equalTo(imageView.mas_height);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).mas_offset(15);
            make.top.mas_equalTo(imageView);
        }];
        [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label);
            make.bottom.mas_equalTo(imageView);
            make.right.mas_lessThanOrEqualTo(-15);
            make.height.mas_equalTo(14);
        }];
        [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.top.mas_equalTo(imageView);
        }];
    }
    return _logisticsView;
}
- (UIView *)customerView {
    if (!_customerView) {
        _customerView = [[UIView alloc] init];
        _customerView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_customerView];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_customer_service_img")];
        [_customerView addSubview:imageView];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"官方客服";
        label.textColor = SKColorWithHex(0x232E45);
        label.font = SKFONT_Medium(15);
        [_customerView addSubview:label];
        UILabel *detailLabel = [[UILabel alloc] init];
        detailLabel.font = SKFONT_SIZE(13);
        detailLabel.textColor = SKColorWithHex(0x999999);
        detailLabel.text = @" ";
        [_customerView addSubview:detailLabel];
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [_customerView addGestureRecognizer:tap];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_offset(15);
            make.bottom.mas_offset(-15);
            make.width.mas_equalTo(imageView.mas_height);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).mas_offset(15);
            make.top.mas_equalTo(imageView);
        }];
        [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label);
            make.bottom.mas_equalTo(imageView);
            make.right.mas_lessThanOrEqualTo(-15);
            make.height.mas_equalTo(14);
        }];
    }
    return _customerView;
}



#pragma mark -- mark EVENT
- (void)tapGesture:(UITapGestureRecognizer *)sender {
    if (sender.view == self.publicNoticeView) {
        // 官方公告
        if (!(self.publicNotisData.count > 0)) {
            [self showMessage:@"暂无消息"];
            return;
        }
        AuctionPubNotisVC *vc = [[AuctionPubNotisVC alloc] init];
        vc.publicNotisData = self.publicNotisData;
        [self.navigationController pushViewController:vc animated:true];
    }else if (sender.view == self.logisticsView) {
        // 物流信息
        if (!(self.logisticsData.count > 0)) {
            [self showMessage:@"暂无物流信息"];
            return;
        }
        AuctionLogisticsVC *vc = [[AuctionLogisticsVC alloc] init];
        vc.logisticsData = self.logisticsData;
        [self.navigationController pushViewController:vc animated:true];
    }else if (sender.view == self.customerView) {
        // 官方客服
        KeFuViewController *vc = [[KeFuViewController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }else {}
}

#pragma mark -- mark 业务逻辑
- (void)refreshPublicData {
    if (self.publicNotisData.count > 0) {
        AuctionMsgPublicNotisInfoModel *info = [self.publicNotisData firstObject];
        _public_des_label.text = info.title;
        _public_date_label.text = [self getDateTitleWithTimeInterval:[info.time integerValue]];;
    }
}
- (void)refreshLogisticsData {
    if (self.logisticsData.count > 0) {
        AuctionLogisticsInfoModel *info = [self.logisticsData firstObject];
        _logistics_des_label.text = info.msg;
        _logistics_date_label.text = [self getDateTitleWithTimeInterval:[info.time integerValue]];
    }
}

/**
 根据时间戳获取时间显示title

 @param timeInterval timeInterval
 @return title
 */
- (NSString *)getDateTitleWithTimeInterval:(NSTimeInterval)timeInterval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    NSString *dateString = @"";
    if (interval > 24 * 60 * 60 && interval < 24 * 2 * 60 * 60) {
        // 昨天
        dateString = @"昨天";
    }else if (interval > 0 && interval < 24 * 60 * 60) {
        // 今天
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm:SS";
        dateString = [NSString stringWithFormat:@"今天 %@", [formatter stringFromDate:date]];
    }else {
        // 前天、以前
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy.MM.dd";
        dateString = [formatter stringFromDate:date];
    }
    return dateString;
}
#pragma mark -- mark 获取网络数据

/**
 获取官方公告
 */
- (void)requestPublicNotisData {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Message/getMessGuanfang" params:@{} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        AuctionMsgPublicNotisModel *model = [AuctionMsgPublicNotisModel modelWithDictionary:response];
        if ([model isSuccess]) {
            weakSelf.publicNotisData = model.data;
            [weakSelf refreshPublicData];
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

/**
 获取物流信息
 */
- (void)requestLogisticData {
    if (!(self.uid && self.loginKey)) {
        return; }
    NSDictionary *param = @{@"uid":@([self.uid intValue]),
                            @"loginKey":self.loginKey };
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Message/getMessWuliu" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        AuctionLogisticsModel *model = [AuctionLogisticsModel modelWithDictionary:response];
        if ([model isSuccess]) {
            weakSelf.logisticsData = model.data;
            [weakSelf refreshLogisticsData];
        }
    } failBlock:^(NSError *error) {
        [self debugShowMessage:error.localizedDescription];
    }];
}

@end
